package com.wuge.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wuge.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wuge.gulimall.product.dao.AttrGroupDao;
import com.wuge.gulimall.product.dao.CategoryDao;
import com.wuge.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wuge.gulimall.product.entity.AttrGroupEntity;
import com.wuge.gulimall.product.entity.CategoryEntity;
import com.wuge.gulimall.product.vo.AttrResVo;
import com.wuge.gulimall.product.vo.AttrVo;
import com.wuge.gulimall.product.vo.SkuItemSaleAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.AttrDao;
import com.wuge.gulimall.product.entity.AttrEntity;
import com.wuge.gulimall.product.service.AttrService;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Resource
private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private AttrGroupDao attrGroupDao;

    @Override
    public List<Long> getSearchAttrBySpuId(List<Long> idList) {
        List<Long> list= getBaseMapper().SearchAttrBySpuId(idList);
    
        return list;
    }

    @Override
    public void save(AttrVo attrVo) {
        AttrEntity entity=new AttrEntity();
        BeanUtils.copyProperties(attrVo,entity);
        this.save(entity);
        if (attrVo.getAttrType()==1&&attrVo.getAttrGroupId()!=null){
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity=new AttrAttrgroupRelationEntity();
       attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        attrAttrgroupRelationEntity.setAttrId(entity.getAttrId());
        attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);}
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long category,String type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (category!=0){
            wrapper.eq("catelog_id",category);
        }

            wrapper.eq("attr_type",type.equalsIgnoreCase("base")?1:0);

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        List<AttrEntity> records = page.getRecords();
        List<AttrResVo> resVos = records.stream().map((entity -> {
            AttrResVo attrResVo = new AttrResVo();
            BeanUtils.copyProperties(entity, attrResVo);
            Long catelogId = entity.getCatelogId();
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            attrResVo.setCatelogName(categoryEntity.getName());
            QueryWrapper<AttrAttrgroupRelationEntity> attr_id = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", entity.getAttrId());
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(attr_id);
            if (attrAttrgroupRelationEntity!=null&&attrAttrgroupRelationEntity.getAttrGroupId()!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
            attrResVo.setGroupName(attrGroupEntity.getAttrGroupName());}

            return attrResVo;
        })).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(resVos);

        return pageUtils;
    }

    @Override
    public void updateAttr(AttrVo attrVo) {
        AttrEntity entity=new AttrEntity();
        BeanUtils.copyProperties(attrVo,entity);
        this.updateById(entity);
        if (attrVo.getAttrType()==1){
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity=new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());

        attrAttrgroupRelationDao.update(attrAttrgroupRelationEntity,new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrVo.getAttrId()));}
    }

    @Override
    public Long getGroupId(Long attrId) {
        QueryWrapper<AttrAttrgroupRelationEntity> attr_id = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(attr_id);
        if (attrAttrgroupRelationEntity!=null) {
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
            return attrGroupEntity.getAttrGroupId();
        }
        return null;

    }

    @Override
    public List<SkuItemSaleAttrVo> getAttrs(Long spuId) {
        List<SkuItemSaleAttrVo> list=  getBaseMapper().getAttrs(spuId);
        return list;
    }
}